package cn.yllr.tomgxianghui.controller;

import cn.yllr.tomgxianghui.entity.Cart;
import cn.yllr.tomgxianghui.entity.Dishes;
import cn.yllr.tomgxianghui.model.ShopCart;
import cn.yllr.tomgxianghui.service.CartService;
import cn.yllr.tomgxianghui.service.DishesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Cart)表控制层
 *
 * @author makejava
 * @since 2021-12-21 19:47:34
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    /**
     * 购物车业务对象
     */
    @Resource
    private CartService cartService;
    /**
     * 菜品表业务对象
     */
    @Resource
    private DishesService dishesService;

    /**
     * 跳转到购物车
     * @param session
     * @param model
     * @return
     */
    @GetMapping("/toCart")
    public String toCart(HttpSession session, Model model) {
        String username = (String) session.getAttribute("username");
        String role = (String) session.getAttribute("role");
        if (username != null && role != null) {
            List<ShopCart> shop_cart = dishesService.queryDishesByUserID(username);
            model.addAttribute("shopcart", shop_cart);
        }
        List<ShopCart> shop_cart = dishesService.queryDishesByUserID(username);
        model.addAttribute("shopcart", shop_cart);
        return "/user/shop_cart";
    }

    /**
     * 通过菜品id添加菜品到购物车
     * @param foodid
     * @param session
     * @return
     */
    @GetMapping("/addToCar")
    @ResponseBody
    public String addToCar(@RequestParam Integer foodid, HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username != null) {
            /*食物id和用户名*/
            Cart cart1 = cartService.queryByUserIdAndFoodId(foodid, username);
            if (cart1 != null) {
                cart1.setCount(cart1.getCount() + 1);
                cartService.update(cart1);
                return "1";
            }
            Dishes dishes = dishesService.queryById(foodid);
            if (dishes != null) {
                Cart cart = new Cart();
                cart.setAccount(username);
                cart.setFoodid(dishes.getFoodid());
                cart.setCount(1);
                cartService.insert(cart);
                return "1";
            }
        }
        return "0";
    }

    /**
     * 设置菜品数量
     * @param foodid 菜品编号
     * @param session
     * @param count 数量
     * @return
     */
    @RequestMapping("/setCount")
    @ResponseBody
    public String setCount(@RequestParam Integer foodid, HttpSession session, Integer count) {
        String username = (String) session.getAttribute("username");
        if (username != null) {
            /*食物id和用户名*/
            ;
            Cart cart1 = cartService.queryByUserIdAndFoodId(foodid, username);
            if (cart1 != null && count >= -1) {
                int i = cart1.getCount() + count;
                if (count == 1 || count == -1 && i > 0) {
                    cart1.setCount(cart1.getCount() + count);
                } else if (count > 1) {
                    cart1.setCount(count);
                }
                cartService.update(cart1);
                return "1";
            }
        }
        return "0";
    }

    /**
     * 通过菜品的id删除购物车里的菜品
     * @param foodid 菜品编号
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/deleteByFoodId")
    @ResponseBody
    public String deleteByFoodid(@RequestParam Integer foodid, HttpSession session, Model model) {
        String username = (String) session.getAttribute("username");
        if (username != null) {
            cartService.deleteByUserIdAndFoodId(foodid, username);
            return "1";
        }
        return "0";
    }
}